/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-10-06
 */
package org.jeecg.modules.flow.callback.inbound.order;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.callback.enums.InboundStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实现任务节点状态处理
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-10-06
 */
@Component
@Slf4j
public class WmsReceiptCompletedBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Autowired
    private InBoundBeanUtils inBoundBeanUtils;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        log.info("任务回调执行了");
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        String id = baseFlowDto.getInfoId();
        WmsInReceiptTask po = iWmsInReceiptTaskService.getById(id);
        //该情况是任务不可分批出入库二次弹窗确认后,需要将任务改为已完成,并且更新节点
        if (DocumentStatusEnum.COMPLETE.getCode().equals(po.getDocumentStatus())){
            po.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
            iWmsInReceiptTaskService.updateById(po);
            baseFlowDto.setRunThenStop(false);
            return;
        }
        if (InboundStatusEnum.PART.getCode().equals(po.getWarehouseStatus())){
            po.setDocumentStatus(DocumentStatusEnum.PROCESS.getCode());
        }
        if (InboundStatusEnum.ALL.getCode().equals(po.getWarehouseStatus())){
            po.setDocumentStatus(DocumentStatusEnum.COMPLETE.getCode());
            //通知mes任务结束
            inBoundBeanUtils.sendMesCompleted(po.getId());
        }
        iWmsInReceiptTaskService.updateById(po);
        if (DocumentStatusEnum.COMPLETE.getCode().equals(po.getDocumentStatus())){
            baseFlowDto.setRunThenStop(false);
        }else {
            //true不更新节点
            baseFlowDto.setRunThenStop(true);
        }
    }
}
